package com.by.service.impl;

import com.by.bean.Inquiry;
import com.by.bean.Product;
import com.by.common.bean.PageBean;
import com.by.common.crud.BaseServiceImpl;
import com.by.common.utils.Assert;
import com.by.common.utils.DateUtils;
import com.by.mapper.InquiryMapper;
import com.by.mapper.ProductMapper;
import com.by.query.InquiryQuery;
import com.by.service.InquiryService;
import com.by.utils.ThreadLocalUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.stereotype.Service;

import javax.validation.constraints.Max;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class InquiryServiceImpl extends BaseServiceImpl<Inquiry, InquiryMapper> implements InquiryService {
    String maxCode;//最大编码
    @Autowired
    InquiryMapper inquiryMapper;
    @Autowired
    ProductMapper productMapper;
    public int save(Inquiry entity) {
        //生成编码
        entity.setQuote_coding(generateNewCode());
        //获取 FreightProduct 列表
        List<Product> products=entity.getFreightProducts();
        //判断列表是否为空
        if(products !=null && !products.isEmpty()){
            //创建新FreightProduct对象
            List<Product> newProducts = new ArrayList<>();
            for (Product product : products) {
                product.setQuote_coding(entity.getQuote_coding());
                Product newProduct = new Product(
                        entity.getQuote_coding(),
                        product.getProduct_number(),
                        product.getProduct_name(),
                        product.getPieces(),
                        product.getGross_weight(),
                        product.getVolume(),
                        product.getSupport_number()
                );
                newProducts.add(newProduct);
            }
            int rs = productMapper.bathSave(newProducts);
            Assert.isZero(rs,"保存产品信息失败");
        }
        return super.save(entity);
    }
    /**
     * 产生新的编码
     *
     * @return
     */
    String generateNewCode() {
        String ym= DateUtils.getSystemTimeYM();
        //判断最大编码是否存在
        if (null == maxCode) {
            //如果不存在  查询数据库
            maxCode = baseMapper.queryMaxCode();
            //如果当前没有编码  说明当前的询价单是当月第一笔
            if (null == maxCode) {
                //产生新的编码
                maxCode = "XJ" + ym + "0001";
                return maxCode;
            }
        }

        //执行到这里  获取最大编码 XJ25020001  计算新的编码
        //提取编码中的日期  2502
        String tmpYm = maxCode.substring(2, 6);
        if (!ym.equals(tmpYm)) {
            //说明当前最大编码不是当月的编码
            maxCode = "XJ" + ym + "0001";
            return maxCode;
        }

        //如果到这里 是当月的最大编码  序号累加 XJ25020001
        String prefix = maxCode.substring(0, 6);
        String seq = maxCode.substring(6);
        int num = Integer.parseInt(seq);
        num++;
        maxCode = prefix + String.format("%04d", num);
        return maxCode;
    }

    /**
     * 分页查询
     * @param query
     * @return
     */

    public PageBean query(InquiryQuery query) {
        PageHelper.startPage(query.getPage(),query.getPageSize());
        List list = inquiryMapper.query(query);
        PageInfo info=new PageInfo(list);
        return new PageBean(query.getPage(),query.getPageSize(),info.getTotal(),list);
    }

    @Override
    public Inquiry get(String quote_coding) {
        return inquiryMapper.get(quote_coding);
    }

}
